//
//  YYNavigationViewController.m
//  NavigationAnimate
//
//  Created by YK on 16/3/30.
//  Copyright © 2016年 LovePlus. All rights reserved.
//

#import "YYNavigationViewController.h"

#import "YYNormalPushTransitionAnimation.h"
#import "YYNormalPopTransitionAnimation.h"



@interface YYNavigationViewController ()<UINavigationControllerDelegate,UIGestureRecognizerDelegate>


@property (strong, nonatomic) UIPercentDrivenInteractiveTransition *percentDrivenInteractiveTransition;

@end

@implementation YYNavigationViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    
    
    self.delegate = self;
    
    
    UIScreenEdgePanGestureRecognizer *edgePanGestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognizer:)];
    edgePanGestureRecognizer.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:edgePanGestureRecognizer];
    
    
    //添加全屏返回手势
    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognizer:)];
    [self.interactivePopGestureRecognizer.view addGestureRecognizer:panGestureRecognizer];

}

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {
    if(operation == UINavigationControllerOperationPush){
        YYNormalPushTransitionAnimation *pushTransition = [YYNormalPushTransitionAnimation new];
        return pushTransition;
    }else if (operation == UINavigationControllerOperationPop){
        YYNormalPopTransitionAnimation *popTransition = [YYNormalPopTransitionAnimation new];
        return popTransition;
    }
    return nil;
}





- (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController {
    if ([animationController isKindOfClass: [YYNormalPopTransitionAnimation class]]) {
        return  self.percentDrivenInteractiveTransition;
    }
    return nil;
}


- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if (self.viewControllers.count > 1) {
        return YES;
    }
    return NO;
}

-(void)panGestureRecognizer:(UIPanGestureRecognizer *)gesture {
    CGFloat progress = [gesture translationInView:gesture.view].x / gesture.view.bounds.size.width;
    if(gesture.state == UIGestureRecognizerStateBegan){
        self.percentDrivenInteractiveTransition = [UIPercentDrivenInteractiveTransition new];
        [self popViewControllerAnimated:YES];
    }else if (gesture.state == UIGestureRecognizerStateChanged){
        [self.percentDrivenInteractiveTransition updateInteractiveTransition:progress];
    }else if (gesture.state == UIGestureRecognizerStateCancelled || gesture.state == UIGestureRecognizerStateEnded){
        if(progress > 0.35){
            [self.percentDrivenInteractiveTransition finishInteractiveTransition];
        }else{
            [self.percentDrivenInteractiveTransition cancelInteractiveTransition];
        }
        self.percentDrivenInteractiveTransition = nil;
    }
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)dealloc {
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
